package com.gin.kotlin.no_gay_alliance.provider.ngaphp

import com.gin.kotlin.no_gay_alliance.argument.TopicListArgument
import com.gin.kotlin.no_gay_alliance.argument.TopicListArgument.Type
import com.gin.nga.main.NgaApi
import com.gin.nga.response.base.NgaResponse
import com.gin.nga.response.topic.TopicListBody
import retrofit2.Call
import java.lang.ref.WeakReference
import java.util.Locale
import java.util.concurrent.TimeUnit

/**
 * 主题列表请求提供者
 */
class TopicListProvider private constructor() : AbstractCachedDataProvider<TopicListArgument, TopicListBody, TopicListBody>(
    TYPE_FACTORY.constructType(TopicListBody::class.java),
    TimeUnit.MINUTES.toSeconds(2)
) {
    override val dir = "TOPIC_LIST"

    override fun transform(rawData: TopicListBody) = rawData

    override fun requestMethod(ngaApi: NgaApi, params: TopicListArgument?): Call<NgaResponse<TopicListBody>> {
        val p = params ?: throw RuntimeException("必须提供参数")
        val recommendOnly = if (p.recommendOnly == true) 1 else null
        val api = ngaApi.topicListApi
        val userApi = ngaApi.userApi
        return when (p.type) {
            Type.FAVOR_FOLDER -> ngaApi.favorApi.list(p.folderId?:1)
            Type.COL_LIST -> api.colList(p.colTid!!, p.page, p.orderByParam())
            Type.COL_SEARCH -> api.colSearch(p.keyword!!, p.colTid!!, p.page, p.searchContent, recommendOnly)
            Type.FORUM_LIST -> api.forumList(p.forumId!!, p.page, p.orderByParam())
            Type.FORUM_SEARCH -> api.forumSearch(p.keyword!!, p.forumIds, p.page, p.searchContent, recommendOnly)
            Type.FORUM_RECOMMEND -> api.forumRecommend(p.forumId!!, p.page)
            Type.USER_SEARCH -> if (p.searchReply == true) {
                userApi.listReply(p.authorUid!!, p.forumId, p.page)
            } else
                userApi.listTopic(p.authorUid!!, p.forumId, p.page, recommendOnly)
        }
    }

    override fun obtainRelativePath(userId: Long, params: TopicListArgument?): String {
        val p = params ?: throw RuntimeException("必须提供参数")
        val recommendOnly = if (p.recommendOnly == true) 1 else null
        val name: String = when (p.type) {
            Type.FAVOR_FOLDER -> String.format(Locale.CHINESE,"%s_%s",p.authorUid,p.folderId)
            Type.COL_LIST -> String.format(Locale.CHINESE, "%d_%d_%s", p.colTid!!, p.page, p.orderByParam()?.name)
            Type.COL_SEARCH -> String.format(Locale.CHINESE, "%s_%d_%d_%d_%d", p.keyword!!, p.colTid!!, p.page, p.searchContent?.value, recommendOnly)
            Type.FORUM_LIST -> String.format(Locale.CHINESE, "%d_%d_%s", p.forumId!!, p.page, p.orderByParam()?.name)
            Type.FORUM_SEARCH -> String.format(Locale.CHINESE, "%s_%s_%d_%d_%d", p.keyword!!, p.forumIds?.joinToString(","), p.page, p.searchContent?.value, recommendOnly)
            Type.FORUM_RECOMMEND -> String.format(Locale.CHINESE, "%d_%d", p.forumId!!, p.page)
            Type.USER_SEARCH -> if (p.searchReply == true) {
                String.format(Locale.CHINESE, "%s_%s_%s", p.authorUid!!, p.forumId, p.page)
            } else
                String.format(Locale.CHINESE, "%s_%s_%s_%s", p.authorUid!!, p.forumId, p.page, recommendOnly)
        }
        return "${p.type.name}/$name.json"
    }


    companion object {
        private var instance: WeakReference<TopicListProvider> = WeakReference(TopicListProvider())
        fun getInstance(): TopicListProvider = instance.get() ?: TopicListProvider().also { instance = WeakReference(it) }
    }
}